package cookingCookingTools

import (
	"github.com/gin-gonic/gin"
	"savor_gin/controllers"
	. "savor_gin/errorType"
	"savor_gin/models"
	"strconv"
)

// 上传信息
func AddTool(c *gin.Context) {
	var recipe models.CookingTools
	if err := c.ShouldBindJSON(&recipe); err != nil {
		controllers.SendResp(c, ParamBindError, nil)
		return
	}
	if err := recipe.Create(); err != nil {
		controllers.SendResp(c, DataBaseError, nil)
		return
	}
	controllers.SendResp(c, OK, recipe)
}

// 更新信息
func UpdateToolInfo(c *gin.Context) {
	toolId, err := strconv.Atoi(c.Param("toolId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	s := &models.CookingTools{ToolID: uint64(toolId)}
	if err := c.ShouldBind(s); err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	if err := s.Update(); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	}
	controllers.SendResp(c, OK, s)
}

// 删除信息
func DeleteTool(c *gin.Context) {
	toolId, err := strconv.Atoi(c.Query("toolId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	s := &models.CookingTools{ToolID: uint64(toolId)}
	if err := s.Delete(); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	}
	controllers.SendResp(c, OK, nil)
}

// 根据id获取信息
func GetToolById(c *gin.Context) {
	toolId, err := strconv.Atoi(c.Query("toolId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	if s, err := models.GetCookingToolsById(uint64(toolId)); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	} else {
		controllers.SendResp(c, OK, s)
	}
}

func GetToolList(c *gin.Context) {
	if s, err := models.GetTools(); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	} else {
		controllers.SendResp(c, OK, s)
	}
}
